let base = '';
let token='';
// let token='eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI2NDg4YThmNGNhNzc0NGM2YjEwZmZlNzZhZDVlNjI3YyIsInN1YiI6IntcInVzZXJJZFwiOjIxNzIsXCJ1c2VyTmFtZVwiOlwic3lzdGVtXCIsXCJsb2dpblRhcmdldFwiOlwiYWRtaW5cIn0iLCJpc3MiOiJzZyIsImlhdCI6MTcwOTUzMjY5MSwiZXhwIjoxNzA5NTkyNjkxfQ.1nRAsHDwjdbTaRj6dXSGhBIu1yHTTqxw9TbyQg5qYH8';
if(localStorage.userInfo){
		token = JSON.parse(localStorage.userInfo).token
}
Api = {
	upload: function (url, data, callback) {
		let headers = {
			'JEECMS-Auth-Token': localStorage.getItem('JEECMS-Auth-Token'),
			'Redirect-Header': false,
			'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundary1kBB8pgsKICPa9RM',
			'X-XSRF-TOKEN':'',
			'Anonymity-Header':(sessionStorage['Anonymity-Header'] || randomWord(false, 6,6)),
		}
		if(token){
			headers.token = token;
		}
		axios({
			method: 'post',
			url: base+url,
			data: data,
			headers: headers
		}).then(function(response) {
			if (response.status == 200) {
				if(response.data.code == '00000 00000' && !url.includes('accessRecord/parse') && !url.includes('accessRecord/updateAccessTime')){
					ElementPlus.ElMessage({
					  message: response.data.message,
					  type: 'success',
					})
				}else if(response.data.code !== '00000 00000'){
					ElementPlus.ElMessage({
					  message: response.data.message,
					  type: 'error',
					})
				}
			
				callback(response.data);
				
			} else {
				ElementPlus.ElMessage({
				  message: response.data.message,
				  type: 'error',
				})
			}
		})

	},
	post: function(url, data, callback) {
		let headers = {
			'JEECMS-Auth-Token': localStorage.getItem('JEECMS-Auth-Token'),
			'Redirect-Header': false,
			'Content-Type': 'application/json',
			'X-XSRF-TOKEN':'',
			'Anonymity-Header':(sessionStorage['Anonymity-Header'] || randomWord(false, 6,6)),
		}
		if(token){
			headers.token = token;
		}
		
		
		axios({
			method: 'post',
			url: base+url,
			data: JSON.stringify(data),
			headers: headers
		}).then(function(response) {
			if (response.status == 200) {
				if(response.data.code == '00000 00000' && !url.includes('accessRecord/parse') && !url.includes('accessRecord/updateAccessTime') && !url.includes('/member/checkPwd') && !url.includes('/member/register') && !url.includes('intelligentform/addViewCnt') && !url.includes('intelligentform/submit')){
					ElementPlus.ElMessage({
					  message: response.data.message,
					  type: 'success',
					})
				}else if(response.data.code !== '00000 00000'){
					ElementPlus.ElMessage({
					  message: response.data.message,
					  type: 'error',
					})
				}
			
				callback(response.data);
				
			} else {
				ElementPlus.ElMessage({
				  message: response.data.message,
				  type: 'error',
				})
			}
		})
	},
	get: function(url, data, callback) {
		let headers = {
			'JEECMS-Auth-Token': localStorage.getItem('JEECMS-Auth-Token'),
			'Redirect-Header': false,
			'Content-Type': 'application/json',
			'X-XSRF-TOKEN':'',
			'Anonymity-Header':(sessionStorage['Anonymity-Header'] || randomWord(false, 6,6)),
		}
		if(token){
			headers.token = token;
		}
		
		axios({
			method: 'get',
			url: base+url,
			params: data,
			headers: headers,
		}).then(function(response) {
			if(response.headers["site-closed"]) {
				if (window.location.origin.includes('127')) {
					window.location.href = window.location.origin + '/frontjeecmsx2/src/page/404.html';
				} else {
					window.location.href = window.location.origin + '/page-404.html';
				}
				return
			}
			if (response.status == 200) {
				if(response.data.code == '00000 00000'){
					
				}else if(response.data.code=='BA061 00005'){
					ElementPlus.ElMessage({
					  message: response.data.message,
					  type: 'error',
					})
				}
				callback(response.data);
			} else {
				ElementPlus.ElMessage({
				  message: response.data.message,
				  type: 'error',
				})
			}
		}).catch(function(error) {
		})
	},
	get_file: function(url, data, callback) {
		axios({
			method: 'get',
			url: base+url,
			params: data,
			responseType: "blob",
		
		}).then(function(response) {
			if (response.status == 200) {
				if(response.data.code == '00000 00000'){
					ElementPlus.ElMessage({
					  message: response.data.message,
					  type: 'success',
					})
				}else{
					// ElementPlus.ElMessage({
					//   message: response.data.message,
					//   type: 'error',
					// })
				}
				
				callback(response)
			} else {
				errorTip(response.data.message)
			}
		}).catch(function(error) {
			console.log(error)
		})
	},
	
}